package edu.bu.met683.TravelDroid.data;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.location.Location;
import android.util.Log;

public class Stats
{
	private static double totalTourDistanceInMiles = 0;
	private static double totalDistanceInMiles = 0;
	private static Calendar startTime = null;
	private Location currentLocation = null;
	private static int totalSeconds = 0;
	public static Calendar currentTime = null; //make sure to set this when the application starts

	
	public Stats (double totalTourDistance)
	{
		totalTourDistanceInMiles = totalTourDistance;
	}
	
	public Stats() {	}
		
	//only want to get the total distance, it is set when updating the current location
	public static double getTotalDistanceInMiles() 
	{
		return totalDistanceInMiles;
	}
	
	public static long getTotalSeconds()
	{
		return totalSeconds;
	}
	
	public static String getCurrentPace()
	{
		UpdateTotalSeconds();
		if(totalSeconds > 0 && totalDistanceInMiles > 0)	
		{
			int secondsPerMile = (int)(totalSeconds / totalDistanceInMiles); 
			int seconds = (int)(secondsPerMile % 60);
			int minutes = (int)((secondsPerMile % 3600) / 60);
			int hours = (int)(secondsPerMile / 3600);
			if(hours != 0 )
				return String.format("%d:%02d:%02d", hours,minutes, seconds) + " /mi.";	
			else
				return String.format("%02d:%02d",minutes, seconds) + " /mi.";	
		}
		else
			return "Not Available";
	}

	public static String getEstimatedFinishTime()
	{
		try
		{
			UpdateTotalSeconds();			
			if(totalDistanceInMiles != 0)
			{
				Calendar estimatedFinishTime = (Calendar)currentTime.clone();
				double remainingDistance = totalTourDistanceInMiles - totalDistanceInMiles;
				int totalEstimatedRemainingSeconds = (int)((totalSeconds / totalDistanceInMiles) * (remainingDistance));
				estimatedFinishTime.add(Calendar.SECOND, totalEstimatedRemainingSeconds);
				SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss a");
				if(estimatedFinishTime != null)
					return dateFormat.format(estimatedFinishTime.getTime());
			}
			return "Not Available";
		}
		catch(Exception e1)
		{
			Log.e("Stats", e1.getMessage());
			return "Not Available";
		}
		
	}
	public Location getLastKnownLocation() {
		return currentLocation;
	}
	private static void UpdateTotalSeconds()
	{
		Calendar now = (Calendar)Calendar.getInstance();
		if (currentTime != null)
		{
			long milliSeconds = now.getTimeInMillis() - currentTime.getTimeInMillis();
			totalSeconds = (int) (totalSeconds + (milliSeconds/1000));
		}
		currentTime = (Calendar)now.clone();
	}
	
	
	public void updateStats(Location newLocation) 
	{
		try
		{
			if(currentLocation == null) //this should only be true for the first call
			{
				currentLocation = new Location("");
				currentLocation.setLatitude(newLocation.getLatitude());
				currentLocation.setLongitude(newLocation.getLongitude());
			}

			double distance = DataUtilities.ConvertMetersToMiles(currentLocation.distanceTo(newLocation));
			this.currentLocation = newLocation;
			totalDistanceInMiles = totalDistanceInMiles + distance;			
			UpdateTotalSeconds();
		}

		
		catch(Exception ee)
		{
			Log.e("Error in code", "Error in stats.UpdateStats");
		}
	}

	public static final String getDistanceDisplay() 

	{
		try
		{
			DecimalFormat df = new DecimalFormat("#.##");
			return df.format(totalDistanceInMiles) + "/" + df.format(totalTourDistanceInMiles) + " mi.";	

		}
		catch(Exception e)
		{
			return "Not Available";
		}
	}

	public static String getStartTime() {
		try
		{
			SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a");
			if(null != startTime)
				return sdf.format(startTime.getTime());
			else
				return "";
		}
		catch (Exception ee)
		{
			return "";
		}
	}

	public void setStartTime(Calendar startTime) {
		Stats.startTime = startTime;
	}

	public Calendar getCurrentTime() {
		return currentTime;
	}

	public void setCurrentTime(Calendar currentTime) {
		Stats.currentTime = currentTime;
	}
	
	public String toString() {
			 
		return  "Start time:  " +Stats.getStartTime().toString() + " " +
				"Total Tour Distance Traveled: " + Stats.getDistanceDisplay() + " " +
				"Average pace: " + Stats.getCurrentPace() + " ";
	}

}
